package com.lh.controller.dev;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh.domain.Category;
import com.lh.domain.Dev_user;
import com.lh.domain.Dictionary;
import com.lh.domain.vo.InfoQuery;
import com.lh.service.CategoryService;
import com.lh.service.Dev_userService;
import com.lh.service.DictionaryService;
import com.lh.service.InfoService;
import com.lh.utils.APKCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author LH
 * @Date 2023/10/25 9:24
 * @PackageName com.lh.controller
 * @Description: [项目名小写 多个单词用_分隔 | 包名全部小写 | 类 首字母大写 | 变量和方法 首字母小写，多个单词组成时除首字母，其他字母首字母需大写 | 常量 全部大写,多个单词用'_'分割]
 */
@Controller
@RequestMapping("/dev")
public class DevMain {

    @Autowired
    private CategoryService category;
    @Autowired
    private DictionaryService dictionary;

    @Autowired
    private InfoService infoService;

    @Autowired
    private Dev_userService userService;

    ObjectMapper mapper = new ObjectMapper();



    //直接进?
    @RequestMapping("/dologin")
    public String doMain(HttpServletRequest request) {
        //获取账号密码进行判断
        String account = request.getParameter("devCode");
        String password = request.getParameter("devPassword");
        Dev_user devUser = userService.accountIsExist(account, password);
        if(devUser != null){
            request.getSession().setAttribute("devUserSession",devUser);
            return "developer/main";
        }else{
            return "devlogin";
        }

    }

    //手游应用维护-列表
    @RequestMapping("/flatform/app/list")
    public String doList(Model model, HttpServletRequest request, @RequestParam(value = "pageIndex",defaultValue = "1") int index) {



        //初始化选框
        List<Category> list = category.getListOne();
        List<Dictionary> appStatus = dictionary.selectValueName("APP_STATUS");
        List<Dictionary> flat = dictionary.selectValueName("APP_FLATFORM");
        model.addAttribute("categoryLevel1List", list);
        model.addAttribute("statusList", appStatus);
        model.addAttribute("flatFormList", flat);
        //初始化列表
        InfoQuery infoQuery = new InfoQuery();


        //配置用户id
        infoQuery.setDevId(1L);
        //软件名称
        infoQuery.setSoftwareName(request.getParameter("querySoftwareName"));
        //状态
        String status = request.getParameter("queryStatus");
        infoQuery.setStatus(status != null && !status.trim().isEmpty() ? Long.valueOf(status) : null);
        //所属平台
        String flatFrom = request.getParameter("queryFlatformId");
        infoQuery.setFlatformId(flatFrom != null && !flatFrom.trim().isEmpty() ? Long.valueOf(flatFrom) : null);
        //一级
        String level1 = request.getParameter("queryCategoryLevel1");
        infoQuery.setCategoryLevel1(level1 != null && !level1.trim().isEmpty() ? level1 : null);

        String level2 = request.getParameter("queryCategoryLevel2");
        infoQuery.setCategoryLevel2(level2 != null && !level2.trim().isEmpty() ? level2 : null);

        String level3 = request.getParameter("queryCategoryLevel3");
        infoQuery.setCategoryLevel3(level3 != null && !level3.trim().isEmpty() ? level3 : null);
        PageHelper.startPage(index,5);
        List<InfoQuery> infoQueries = infoService.selectAllInfo(infoQuery);
        PageInfo pageInfo = new PageInfo(infoQueries);
        model.addAttribute("appInfoList",infoQueries);
        model.addAttribute("pages",pageInfo);
        //测试分页

        return "developer/appinfolist";
    }

    @RequestMapping("flatform/app/categorylevellist")
    @ResponseBody
    public String showList(Model model, HttpServletRequest request) throws JsonProcessingException {
        System.out.println("获取前端请求:" + request.getParameter("pid"));
        List<Category> list = null;

        String pid = request.getParameter("pid");
        if (pid != null && !pid.trim().isEmpty()) {
            list = category.getList(Integer.parseInt(pid));
        } else {
            list = category.getListOne();
        }
        return mapper.writeValueAsString(list);
    }

    @RequestMapping("/flatform/app/appinfoadd")
    public String addApp() {
        return "developer/appinfoadd";
    }


    //判断名称
    @RequestMapping("/flatform/app/apkexist")
    @ResponseBody
    public String chooseAPKName(HttpServletRequest request) throws JsonProcessingException {
        System.out.println("已进入查询!");
        String apkName = request.getParameter("APKName");
        APKCode apkCode = infoService.selectAPKName(apkName);
        System.out.println("获取值:" + apkCode.getAPKName());
        return mapper.writeValueAsString(apkCode);
    }


}
